import Vue from 'vue'
import VueRouter from 'vue-router'
import uiLogin from '@/views/ui-login.vue'
import uiHome from '@/views/ui-home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: uiLogin
  },
  {
    path: '/home',
    component: uiHome,
    children: [
      {
        path: '',
        component: () => import('@/views/home/Basic System Management/ui-home.vue')
      },
      {
        path: 'uiUser',
        component: () => import('@/views/home/Basic System Management/ui-user.vue')
      },
      {
        path: 'UiPermissionManagement',
        component: () => import('@/views/home/Basic System Management/ui-permission-management.vue')
      },
      {
        path: 'loginLog',
        component: () => import('@/views/home/ui-log/ui-login-log.vue')
      },
      {
        path: 'operationLog',
        component: () => import('@/views/home/ui-log/ui-operation-log.vue')
      },
      {
        path: 'eventManagement',
        component: () => import('@/views/home/Content management/ui-event-management.vue')
      },
      {
        path: 'classification',
        component: () => import('@/views/home/Content management/ui-classification.vue')
      },
      {
        path: 'videoManagement',
        component: () => import('@/views/home/Content management/ui-video-management.vue')
      },
      {
        path: 'contenFeedback',
        component: () => import('@/views/home/ui-conten-feedback.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach((to, from, next) => {
  // 如果访问登录页，直接放行
  if (to.path === '/login') {
    next()
    return
  }
  // 获取token
  const token = localStorage.getItem('token')
  if (!token) {
    next('/login')
  } else {
    next()
  }
})

export default router
